package week4.day22;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

/**
 * @Author fengzihao
 * @Date 2024-05-06 19:42
 */
public class IOPracticeWithWhile {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("请输入文件地址：");
            String name = scanner.nextLine();
            if ("end".equals(name))
                break;
            System.out.println("请输入要存储的字符串：");
            String str = scanner.nextLine();
            File file = new File(name);
            if (!file.exists()) {
                try {
                    file.createNewFile();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            try (FileOutputStream fileOutputStream = new FileOutputStream(file,true)) {
                fileOutputStream.write(str.getBytes("UTF-8"));
                fileOutputStream.write("\n".getBytes(StandardCharsets.UTF_8));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
